﻿using Microsoft.Extensions.Logging;
using Naruto.Subscribe.Extension;
using Naruto.TaskManagement.Entitys.DTO;
using Naruto.TaskManagement.Infrastructure;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Naruto.TaskManagement.Application.HttpClientApps
{
    /// <summary>
    /// 授权中心的http客户端
    /// </summary>
    public class AuthCenterHttpClient
    {
        private readonly HttpClient httpClient;

        private readonly ILogger logger;

        public AuthCenterHttpClient(HttpClient _httpClient, ILogger<AuthCenterHttpClient> _logger)
        {
            httpClient = _httpClient;
            logger = _logger;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> UpdatePassword(string userId, UpdatePasswordDTO info)
        {
            using var res = await httpClient.PutAsync($"/api/user/updpswd/{userId}", GetJsonContent(info));
            return await BuildReturn(res);
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> Register(RegisterDTO info)
        {
            using var res = await httpClient.PostAsync($"/api/user/register", GetJsonContent(info));
            return await BuildReturn(res);
        }

        /// <summary>
        /// 根据邮箱重置密码
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<NarutoResult> ResetPswdByEmail(ResetPswdByEmailDTO info)
        {
            using var res = await httpClient.PostAsync($"/api/user/resetbyemail", GetJsonContent(info));
            return await BuildReturn(res);
        }
        /// <summary>
        /// 获取json内容
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private HttpContent GetJsonContent(object obj)
        {
            return new StringContent(obj.ToJsonString(), Encoding.UTF8, "application/json");
        }
        /// <summary>
        /// 构建返回内容
        /// </summary>
        /// <param name="httpResponseMessage"></param>
        /// <returns></returns>
        private async Task<NarutoResult> BuildReturn(HttpResponseMessage httpResponseMessage)
        {
            //获取返回的内容
            var result = await httpResponseMessage.Content.ReadAsStringAsync();
            if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return result.ToDeserialized<NarutoSuccessResult>();
            }
            else
            {
                logger.LogWarning("请求错误,接口地址{url},参数{para},StatusCode={StatusCode}", httpResponseMessage.RequestMessage.RequestUri.ToString(), await httpResponseMessage.RequestMessage.Content.ReadAsStringAsync(), httpResponseMessage.StatusCode);
                return new NarutoFailResult(result);
            }
        }
    }
}
